#ifndef HIGHLIGHTER_H
#define HIGHLIGHTER_H
//QTextDocument利用Highlighter类来完成代码的高亮显示
class Highlighter : public QSyntaxHighlighter
{
public:
    Highlighter(QTextDocument *parent = nullptr);
    QTextCharFormat getOperatorFormat()
    {
        return operatorFormat;
    }

protected:
    void highlightBlock(const QString &text) override;

private:
    struct HighlightingRule
    {
        QRegularExpression pattern;
        QTextCharFormat format;
    };

    QList<HighlightingRule> highlightingRules;

    QRegularExpression singleCommentStartExp = QRegularExpression(QStringLiteral("//"));
    QRegularExpression singleCommentEndExp = QRegularExpression(QStringLiteral(""));
    QRegularExpression multiCommentStartExp = QRegularExpression(QStringLiteral("/\\*"));
    QRegularExpression multiCommentEndExp = QRegularExpression(QStringLiteral("\\*/"));
    QRegularExpression stringStartExp = QRegularExpression(QStringLiteral("(?<!\\\\)\""));
    QRegularExpression stringEndExp = QRegularExpression(QStringLiteral("(?<!\\\\)\""));
    QRegularExpression charStartExp = QRegularExpression(QStringLiteral("(?<!\\\\)\'"));
    QRegularExpression charEndExp = QRegularExpression(QStringLiteral("(?<!\\\\)\'"));
    QRegularExpression rawStringStartExp = QRegularExpression(QStringLiteral("R\"\\("));
    QRegularExpression rawStringEndExp = QRegularExpression(QStringLiteral("\\)\""));
    QRegularExpression numberExpression = QRegularExpression(QStringLiteral(R"(\b\d+(\.\d*)?[fF]?((?<![fF])[Ee][\+\-]?\d+)?\b)"));
    QRegularExpression hexExpression = QRegularExpression(QStringLiteral("\\b0[xX][0-9a-fA-F]+\\b"));

    QList<QRegularExpression> startExpList{charStartExp, stringStartExp, rawStringStartExp, multiCommentStartExp, singleCommentStartExp};
    QList<QRegularExpression> endExpList{charEndExp, stringEndExp, rawStringEndExp, multiCommentEndExp, singleCommentEndExp};

    QTextCharFormat stringFormat;
    QTextCharFormat operatorFormat;
    QTextCharFormat singleLineCommentFormat;
    QTextCharFormat multiLineCommentFormat;

    QRegularExpression getEndExpFromStartExp(QRegularExpression startExp);
};
#endif
